package com.pay.paytoken.base;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.jaeger.library.StatusBarUtil;
import com.pay.paytoken.R;
import com.pay.paytoken.activity.lock.LockManager;
import com.pay.paytoken.listener.BaseListener;

import appUtils.ActivityUtil;
import appUtils.AppUtil;
import appUtils.CustomProgressDialog;
import appUtils.LogUtil;
import appUtils.PhoneUtil;
import appUtils.SharedPreferencesManager;
import appUtils.ToastUtil;
import butterknife.ButterKnife;
import constant.AppConstants;

import static com.pay.paytoken.paytoken.PayTokenConstant.diffTime;
import static com.pay.paytoken.paytoken.PayTokenConstant.hasStop;
import static com.pay.paytoken.paytoken.PayTokenConstant.onStopTime;

public abstract class BaseActivity extends AppCompatActivity implements BaseListener {

    protected static SharedPreferencesManager mSharedPreferencesManager;
    private CustomProgressDialog mProgressDialog;
    protected PhoneUtil mPhoneUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setView());
        ButterKnife.bind(this);
        AppUtil.getInstance().pushAC(this);
        init();
        initData(savedInstanceState);
        initView();
    }

    private void init() {
        initSharePreferences();
        //setStatusBar
        StatusBarUtil.setColor(this, ContextCompat.getColor(getApplicationContext(), R.color.colorGreen));
        mPhoneUtil = new PhoneUtil(this);
    }

    private void initSharePreferences() {
        if (mSharedPreferencesManager == null) {
            mSharedPreferencesManager = SharedPreferencesManager.getInstance(getApplicationContext());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        lock();
    }

    private void lock() {
        if (getClass().getSimpleName().equals("WelcomeActivity") || getClass().getSimpleName().equals("PasswordVerificationActivity")) {
            return;
        }
        if (LockManager.getInstance().getLockAble()) {
            long onResumeTime = System.currentTimeMillis();
            if ((onResumeTime - onStopTime) >= diffTime) {
                if (hasStop) {
                    LockManager.getInstance().startLockActivity(this);
                    LogUtil.log("hasStop: " + (onResumeTime - onStopTime));
                }
            }
            hasStop = false;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (getClass().getSimpleName().equals("WelcomeActivity") || getClass().getSimpleName().equals("PasswordVerificationActivity")) {
            return;
        }

        if (LockManager.getInstance().getLockAble()) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            boolean isScreenOn = pm.isScreenOn();
            if (!isScreenOn) {
                LockManager.getInstance().startLockActivity(this);
                return;
            }
            if (!ActivityUtil.isForeground(this, getClass().getName())) {
                if (AppConstants.isAppFinish) {
                    return;
                }
                ToastUtil.showShort(R.string.CFTToken_run_background);
                onStopTime = System.currentTimeMillis();
                hasStop = true;
            } else {
                hasStop = false;
            }
        }
        if (getClass().getSimpleName().equals("MyConfirmPatternActivity")) {
            hasStop = false;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        AppUtil.getInstance().removeAC(this);
    }

    String getTopActivity() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
        String shortClassName = info.topActivity.getShortClassName(); //类名
        int i = shortClassName.lastIndexOf(".");
        return shortClassName.substring(i + 1, shortClassName.length());
    }

    protected void showProgress(final int resID) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog == null) {
                    mProgressDialog = new CustomProgressDialog(BaseActivity.this, getResources().getString(resID));
                }
                mProgressDialog.show();
            }
        });
    }

    protected void dismisProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPhoneUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
